/*
 * TOP SECRET Copyright 2006-2015 Transsion.com All right reserved. This software is the confidential and proprietary
 * information of Transsion.com ("Confidential Information"). You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement you entered into with Transsion.com.
 */
package com.yunji.framework_template.biz.crawler.cd;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yunji.framework_template.biz.crawler.ContentImageHandler;
import com.yunji.framework_template.biz.crawler.NewsCrawler;
import com.yunji.framework_template.biz.crawler.SourceType;
import com.yunji.framework_template.common.enumeration.CountryCode;
import com.yunji.framework_template.common.enumeration.NewsType;

/**
 * ClassName:NationNewsCrawler <br/>
 * Date: 2018年12月18日 下午3:31:24 <br/>
 * 
 * @author fenglibin1982@163.com
 * @Blog http://blog.csdn.net/fenglibing
 * @version
 * @see
 */
@Service
public class IndependantNewsCrawler extends NewsCrawler {

    @Resource
    private IndependantNewsImageHandler independantNewsImageHandler;

    @Override
    public boolean isOkUrl(String url) {
        // 新闻详情url格式如：https://citizentv.co.ke/news/10yr-old-girl-found-hanging-from-a-rope-in-rarieda-223708/
        // 通过判断url的path中是否包括年月来判断其是否为新闻详情页
        URI uri;
        try {
            if (url == null || url.trim().length() == 0) {
                return false;
            }
            if (url.startsWith("mailto") || url.toLowerCase().startsWith("javascript")) {
                return false;
            }
            uri = new URI(url);
            String host = uri.getHost();
            if(host==null) {
                return false;
            }
            if (host.indexOf("congoindependant.com") < 0) {
                return false;
            }
            String path = uri.getPath();
            String[] pathArr = path.split("/");
            if (pathArr.length == 2 && pathArr[1].split("-").length >= 2) {
                return true;
            }
        } catch (URISyntaxException e) {
        }
        return false;
    }

    @Override
    public ContentImageHandler getContentImageHandler() {
        return independantNewsImageHandler;
    }

    @Override
    public List<SourceType> getSourceTypeList() {
        List<SourceType> sourceTypeList = new ArrayList<SourceType>();
        sourceTypeList.add(SourceType.builder().url("http://www.congoindependant.com/").newsType(NewsType.HOT).build());
        sourceTypeList.add(SourceType.builder().url("http://www.congoindependant.com/category/edito/").newsType(NewsType.EDUCATION).build());
        sourceTypeList.add(SourceType.builder().url("http://www.congoindependant.com/category/actualites/").newsType(NewsType.VIDEO).build());
        sourceTypeList.add(SourceType.builder().url("http://www.congoindependant.com/category/depeches/").newsType(NewsType.IMAGE).build());
        sourceTypeList.add(SourceType.builder().url("http://www.congoindependant.com/category/communique/").newsType(NewsType.SPORT).build());
        sourceTypeList.add(SourceType.builder().url("http://www.congoindependant.com/category/opinion-debat/").newsType(NewsType.LIFESTYLE).build());
        sourceTypeList.add(SourceType.builder().url("http://www.congoindependant.com/category/questions-directes-a/").newsType(NewsType.FUN).build());
        sourceTypeList.add(SourceType.builder().url("http://www.congoindependant.com/category/nos-confreres/").newsType(NewsType.HOUSE).build());
        sourceTypeList.add(SourceType.builder().url("http://www.congoindependant.com/category/tout-kin/").newsType(NewsType.ASKANDANSWER).build());
        sourceTypeList.add(SourceType.builder().url("http://www.congoindependant.com/category/presse-anglophone/").newsType(NewsType.HEALTH).build());
        sourceTypeList.add(SourceType.builder().url("http://www.congoindependant.com/about/").newsType(NewsType.MILITARY).build());
        sourceTypeList.add(SourceType.builder().url("http://www.congoindependant.com/charte/").newsType(NewsType.CAR).build());
        sourceTypeList.add(SourceType.builder().url("http://www.congoindependant.com/redaction/").newsType(NewsType.TECH).build());

        return sourceTypeList;
    }

    @Override
    public Set<String> getCountryCodeSet() {
        Set<String> countryCodeSet = new HashSet<String>();
        countryCodeSet.add(CountryCode.CD.name());
        return countryCodeSet;
    }
}
